<template>
<div>
	<div class="g_top_box">
		<div class="g_right_box">
			<Button @click="download" style="margin-right:10px;">下载案例</Button>
			<Upload
				ref="upload"
				:action="upload_url"
				:on-success="handleSuccess"
				:max-size="20480"
				:on-format-error="handleFormatError"
				:on-exceeded-size="handleMaxSize"
				:show-upload-list="false"
				style="    display: inline-block;"
				>
				<Button type="primary" style="margin-right:20px;">导入数据</Button>
			</Upload>
			
			
			
			<Button type="primary" @click="$router.push({name:'subject_type'})">科目类型</Button>
			<Button type="primary" @click="add">添加</Button>
		</div> 
		<div class="g_left_box"><h4>科目管理</h4></div>
	</div>
	
	<div class="g_list_box">
		<div class="g_filter_box">
			<Form ref="formDynamic" :label-width="50" style="display: inline-block;">
				<Input v-model="filter.name" placeholder="名称" style="width:130px;"></Input>
				<Select v-model="filter.type" style="width:130px">
					<Option :value="0">全部</Option>
					<Option v-for="item in typelist" :value="item.id" :key="item.id">{{ item.name }}</Option>
				</Select>
				<Button type="primary" @click="qurey">搜索</Button>
			</Form>
			<div class="export_box" style="float:right;">
				<Button type="primary" style="margin-right:20px;" @click="export_data">导出数据</Button>
			</div>
		</div>
		<div class="g_list_box">
			<Table :columns="Columns" :data="list" style="width: 100%;"></Table>
		</div>
	</div>
	
	<Modal
		v-model="info.show"
		title="科目信息"
		@on-ok="info_ok"
		>
		<div>
			<Form :label-width="80">
				<FormItem label="名称">
					<Input type="textarea" v-model="info.info.name" style="width:220px;"></Input>
				</FormItem>
				<FormItem label="类型">
					<Select v-model="info.info.type" style="width:220px;">
						<Option v-for="item in typelist" :value="item.id" :key="item.id">{{ item.name }}</Option>
					</Select>
				</FormItem>
			</Form>
			
		</div>
	</Modal>
	
	
</div>
</template>

<script>
export default {
    name: 'bumen_list',
    data () {
		let self = this;
        return {
            Columns: [
				{
					title: 'ID',
					key: 'id',
					align: 'center',
                    width: 80
                },
				{
                    title: '名称',
					key: 'name',
					width: 300,
                },
				{
                    title: '类型',
					key: 'type_name',
					width: 200,
                },
				{
                        title: '操作',
                        key: 'action',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small',
										icon:'edit'
                                    },
                                    style: {
                                        marginRight: '5px',
                                    },
                                    on: {
                                        click: () => {
											self.edit(params.row);
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small',
										icon:'trash-a'
                                    },
									style: {
										display:params.row.username =='admin'?'none':'inline-block'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.row.id)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
            ],
            list: [],
			filter:{
				name:'',
				type:0
			},
			id:0,
			info:{
				show:false,
				info:{
					name:'',
					type:0
				}
			},
			typelist:[],
			imports:{
				show:false,
				mark:'',
			},
			upload_url:Whttp.upload_url
        };
    },
	created(){
		this.id = this.$route.params.id;
		this.load_data();
		this.load_type();
	},
	methods:{
		load_data(){
			var self = this;
			var postdata = this.filter;
			
			Whttp.load('User/Subject/get_list',postdata,function(result){
				self.list = result;
			});
		},
		qurey(){
			this.load_data();
		},
		add(){
			this.info.info = {
				name:'',
				type:0
			};
			this.info.show = true;
		},
		edit(val){
			this.info.info = val;
			this.info.show = true;
			
		},
		info_ok(){
			var self = this;
			var postdata = {
				name:this.info.info.name,
				type:this.info.info.type,
				id:this.info.info.id,
				is_more:1
			};
			
			Whttp.load('User/Subject/save',postdata,function(result){
				self.load_data();
			});
		},
		remove(id){
			if(!confirm('是否删除')){
				return;
			}
			var self = this;
			var postdata = {
				id:id
			};
			Whttp.load('User/Subject/del',postdata,function(result){
				self.load_data();
			});
		},
		load_type(){
			var self = this;
			var postdata = {
			}
			
			Whttp.load('User/Subject/type_get_list',postdata,function(result){
				self.typelist = result;
			});
		},
		add_type(name){
			var self = this;
			var postdata = {
				name:name
			}
			
			Whttp.load('User/Subject/type_add',postdata,function(result){
				self.load_type();
			});
		},
		del_type(id){
			var self = this;
			var postdata = {
				id:id
			}
			
			Whttp.load('User/Subject/type_del',postdata,function(result){
				self.load_type();
			});
		},
		import_upload(mark){
			var self = this;
			var postdata = {
				mark:mark
			};
			Whttp.load('User/Subject/import_data',postdata,function(result){
				alert('导入成功');
				self.load_data();
			},function(result){
				alert(result.message);
			});
		},
		handleSuccess (res, file) {
			
			if(res.result_code==0){
				var ls_file = res.data[0];
				
				this.import_upload(ls_file.mark);
			}
			
		},
		handleFormatError (file) {
			this.$Notice.warning({
				title: 'The file format is incorrect',
				desc: 'File format of ' + file.name + ' is incorrect, please select jpg or png.'
			});
		},
		handleMaxSize (file) {
			this.$Notice.warning({
				title: 'Exceeding file size limit',
				desc: 'File  ' + file.name + ' is too large, no more than 2M.'
			});
		},
		download(){
			Whttp.load('Chuna/Cash/download',{name:'subject'},function(result){
				window.open(result);
			});
		},
		export_data(){
			var self = this;
			var postdata = this.filter;
			
			Whttp.load('User/Subject/export_data',postdata,function(result){
				window.open(result);
			},function(result){
				alert(result.message);
			});
		}
	}
};
</script>